﻿
@{
    ViewBag.Title = "集采任务维护";
}
<div class="layui-card" style="margin-top: 15px;">
    <div class="layui-card-header" style="padding-bottom:10px;padding-top:10px;">
        <span style="margin-left:10px;margin-right:100px;font-size:large;font-weight:600;">@Model.DrugName [@Model.GpName]</span>
        <button id="doSave" class="layui-btn submit">保存</button>
        <button id="onCancel" class="layui-btn layui-btn-danger" type="button">关闭</button>
    </div>
    <div class="layui-row layui-col-space10">
        <div class="layui-col-md4">
            <div class="layui-transfer-search" style="border-width: 1px;">
                <i class="layui-icon layui-icon-search"></i>
                <input id="leftsearch" type="text" class="layui-input" placeholder="搜索" autocomplete="off" />
            </div>
            <table id="left_tab" lay-filter="left"></table>
        </div>
        <div class="layui-col-md1 btn-height">
            <div style="margin-bottom: 10px;">
                <button class="layui-btn  layui-btn-disabled left-btn">
                    <i class="layui-icon">&#xe602;</i>
                </button>
            </div>
            <div>
                <button class="layui-btn layui-btn-disabled right-btn">
                    <i class="layui-icon">&#xe603;</i>
                </button>
            </div>
        </div>
        <div class="layui-col-md7">
            <table id="right_tab" lay-filter="right"></table>
        </div>
    </div>
</div>
<script type="text/javascript" src="~/Scripts/jsonDB.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'admin'], function () {
        var table = layui.table, $ = layui.$, admin = layui.admin;;
        var height = 450; //固定表格高度
        //计算按钮的高度
        var btn_height = height / 2 - 44;
        $('.btn-height').css('padding-top', btn_height).css('text-align', 'center');
        //左边表格
        var enable = '@ViewData["enable"]';
        if (enable == "1") {
            $("#doSave").hide();
            $(".left-btn").hide();
            $(".right-btn").hide();
        }
        admin.req({
            url: '/DeptMergeInfo/GetMissionDeptList' //实际使用请改成服务端真实接口
            , method: 'post'
            , data: "gpCode=@Request["GpCode"]&drugCode=@Request["DrugCode"]"
            , done: function (res) {
                //curleftdata = res.data;
                jsonDB(res.data, 'leftdata').init('leftDB');
                table.render({
                    elem: '#left_tab'
                    , data: res.data
                    , cols: [[
                        { checkbox: true, fixed: true }
                        , { field: 'DeptCode', title: '科室编码', width: 80 }
                        , { field: 'DeptName', title: '科室名称', width: 160 }
                    ]]
                    , id: 'left_table_id'
                    , page: true
                    , limit: 1000
                    , height: height
                });
            }
        });
        admin.req({
            url: '/GroupMission/GetList' //实际使用请改成服务端真实接口
            , method: 'post'
            , data: "gpCode=@Request["GpCode"]&drugCode=@Request["DrugCode"]"
            , done: function (res) {
                jsonDB(res.data, 'rightdata').init('rightDB');
                table.render({
                    elem: '#right_tab'
                    , data: res.data
                    , cols: [[
                        { checkbox: true, fixed: true }
                        , { field: 'DeptCode', title: '科室编码', width: 80, totalRowText: '合计：' }
                        , { field: 'DeptName', title: '科室名称', width: 120 }
                        , { field: 'Unit', title: '单位', width: 80 }
                        , { field: 'Qyt', title: '完成任务量', width: 100, edit: 'text', totalRow: true }
                        , { field: 'Rate', title: '完成比例', width: 100, edit: 'text' }

                    ]]
                    , id: 'right_table_id'
                    , page: false
                    , limit: 1000,
                    totalRow: true
                    , height: height
                });
            }
        });

        $("#leftsearch").on('input', function () {
            var val = $(this).val();
            var result = leftDB.findAll("leftdata");
            var ldata = result.filter(e => {
                return e.DeptCode.indexOf(val) >= 0 || e.DeptName.indexOf(val) >= 0 || (e.SpellCode != undefined && e.SpellCode.toLowerCase().indexOf(val.toLowerCase()) >= 0)
        });
            reloadleftTable(ldata);
        });
        //公共方法
        function getTableData(id) {
            var checkStatus = table.checkStatus(id)
                , data = checkStatus.data;
            return data;
        }
        function btnIf(data, btn) {
            if (data && data.length) {
                $(btn).removeClass('layui-btn-disabled');
            } else {
                $(btn).addClass('layui-btn-disabled');
            }
        }

        function reloadleftTable(ldata) {
            table.reload('left_table_id', {
                data: ldata
            });
        }

        function reloadrightTable(rdata) {
            table.reload('right_table_id', {
                data: rdata
            });
        }
        //监听左表格选中
        table.on('checkbox(left)', function (obj) {
            btnIf(getTableData('left_table_id'), '.left-btn');
        });
        //监听右表格选中
        table.on('checkbox(right)', function (obj) {
            btnIf(getTableData('right_table_id'), '.right-btn');
        });
        //左按钮点击移动数据
        $('.left-btn').on('click', function () {
            if (!$(this).hasClass('layui-btn-disabled')) {
                $('.left-btn,.right-btn').addClass('layui-btn-disabled');
                var data = getTableData('left_table_id');
                for (n in data) {
                    leftDB.query('delete from leftdata where (DeptCode="' + data[n].DeptCode + '") limit 1');
                    rightDB.table('rightdata').addFirst({ GpCode: '@Request["GpCode"]', DrugCode: '@Request["DrugCode"]', DeptCode: data[n].DeptCode, DeptName: data[n].DeptName,Unit:'@Model.PackUnit' });
                }
                reloadleftTable(leftDB.findAll("leftdata"));
                reloadrightTable(rightDB.findAll("rightdata"));
            }
        });
        //右按钮点击移动数据
        $('.right-btn').on('click', function () {
            if (!$(this).hasClass('layui-btn-disabled')) {
                $('.left-btn,.right-btn').addClass('layui-btn-disabled');
                var data = getTableData('right_table_id');
                for (n in data) {
                    var rdata = rightDB.findAll("rightdata").filter(e => { return e.DeptCode == data[n].DeptCode });
                    for (t in rdata) {
                        rightDB.query('delete from rightdata where (DeptCode="' + rdata[t].DeptCode + '") limit 1');
                    }
                    leftDB.table('leftdata').addFirst({ DeptCode: data[n].DeptCode, DeptName: data[n].DeptName });
                }
                reloadrightTable(rightDB.findAll("rightdata"));
                reloadleftTable(leftDB.findAll("leftdata"));
            }
        });

        $('#doSave').on('click', function () {
            var data = rightDB.findAll("rightdata");
            var valid = true;
            for (var i = 0; i < data.length; i++) {
                if (data[i].Qyt == "" || data[i].Qyt == "0" || data[i].Rate == "" || data[i].Rate == "0") {
                    valid = false;
                }
            }
            if (!valid) {
                layer.alert("完成数量和完成比例不能为空");
                return;
            }
            $.ajax({
                url: '/GroupMission/SaveMission',
                type: 'post',
                dataType: 'json',
                data: { items: data , gpCode: '@Request["GpCode"]',drugCode:'@Request["DrugCode"]'},
                success: function (res) {
                    layer.alert(res.msg);
                }
            });
        });
        $('#onCancel').on('click', function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
    });
</script>
